package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    DishMapper dishMapper;

    /**
     * 新增套餐和套餐包含的菜品
     */
    @Transactional
    @Override
    public void AddSetmealWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //添加套餐
        setmealMapper.AddSetmeal(setmeal);

        Long setmealId = setmeal.getId();

        //对套餐添加对应的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //设置菜品的套餐id
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishMapper.AddSetmeal(setmealDishes);
    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult PageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //调取分页插件
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        //开始分页
        Page<SetmealVO> setmealVOList = setmealMapper.PageQuery(setmealPageQueryDTO);
        return new PageResult(setmealVOList.getTotal(), setmealVOList.getResult());
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            //根据id查询套餐，判断是否在起售中
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

//        for (Long id : ids) {
//            //将套餐和关联的菜品删除 法一
//            setmealMapper.deleteBySetmealId(id);
//            setmealDishMapper.deleteDishsBySetmealId(id);
//        }

        //删除套餐对应的id 法二
        setmealDishMapper.deleteDishsBySetmealId(ids);
        //删除套餐
        setmealMapper.deleteBySetmealIds(ids);

    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO GetByIdWithDish(Long id) {
        //在实体类中根据 id查询
        Setmeal setmeal = setmealMapper.GetSetmealById(id);
        //根据SetmealId查询出 setmeal_dish表中 和套餐关联的菜品
        List<SetmealDish> setmealDishList = setmealDishMapper.getDishsBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        //利用 属性拷贝将 其填入到vo中
        BeanUtils.copyProperties(setmeal, setmealVO);
        //赋值套餐对应的菜品
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 修改套餐信息
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void UpdateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //修改套餐的基本信息
        setmealMapper.UpdateSetmeal(setmeal);

        //套餐id
        Long setmealId = setmeal.getId();

        //将套餐对应的菜品 全部删除
        setmealDishMapper.deleteDishBySetmealId(setmealId);

        //将套餐对应的每个菜品 添加对应的套餐id
        List<SetmealDish> dishList = setmealDTO.getSetmealDishes();
        for (SetmealDish dish : dishList) {
            dish.setSetmealId(setmealId);
        }

        //添加对应的 菜品
        setmealDishMapper.AddSetmeal(dishList);
    }

    /**
     * 套餐起售、停售
     *
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if (status == StatusConstant.ENABLE) {
            //select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if (dishList != null && dishList.size() > 0) {
                dishList.forEach(dish -> {
                    if (StatusConstant.DISABLE == dish.getStatus()) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.UpdateSetmeal(setmeal);
    }

    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     *
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
